package com.example.deptempmanageservice.controller;

import com.myprojects.common.pojo.Department;
import com.myprojects.common.pojo.Position;
import com.example.deptempmanageservice.service.PositionService;
import com.github.pagehelper.PageInfo;
import com.myprojects.common.dto.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@Controller
@RequestMapping(value = "/position-curd")
public class PositionController {
    @Autowired
    PositionService positionService;

    /**
     * 该方法用于分页查询职位信息
     */
    @RequestMapping(value = "/positions", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectPosition(
            @RequestParam(value = "pn", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "size", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "search", defaultValue = "") String searchStr) {//普通
        Msg result;//要返回的结果
        List<Department> departmentList;//带职位的部门列表(要返回的数据)
        Integer id = null;
        int total;//查询结果总数
        try {
            PageInfo pageInfo = positionService.selectBySearchStr(pageNum, pageSize, searchStr);

            //生成返回成功的结果
            result = Msg.succeed();
            //将pageInfo加入返回的结果中
            result.addData("pageInfo", pageInfo);
            System.out.println(result.getDataMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = Msg.failed();
            result.setMsg("服务端处理异常!");
        }

        return result;
    }

    /**
     * 该方法用于获取全部部门的列表(带职位列表)
     */
    @RequestMapping(value = "/get-all-departments-with-positions", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectAllPositions() {
        Msg result = null;
        try {
            List<Department> departments = positionService.selectAllPosition();
            //生成返回成功的结果
            result = Msg.succeed();
            result.addData("departments", departments);
            System.out.println(result.getDataMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = Msg.failed();
            result.setMsg("服务端处理异常!");
        }
        return result;
    }

    /**
     * 该方法用于保存职位的信息
     */
    @RequestMapping(value = "/positions", method = RequestMethod.POST)
    @ResponseBody
    public Msg savePosition(@Valid @RequestBody Position position, BindingResult result) {
        Msg msg = null;
        System.out.println("position:" + position);
        //先检查部门信息有没有错误
        Map<String, Object> errorMap = positionService.checkPosition(position, result);
        System.out.println("传入的savePosition为:" + position + "\nerrorMap:" + errorMap);
        //如果都无误，则尝试保存
        if (errorMap.size() < 1) {
            int iSaveResult = positionService.insertSelective(position);
            if (iSaveResult > 0) {
                msg = Msg.succeed();
                msg.addData("position_id", position.getId());
                msg.setMsg("保存职位信息成功！");
            } else {
                msg = Msg.failed();
                msg.setMsg("保存职位信息失败！");
            }
        } else {
            msg = Msg.failed();
            msg.setMsg("保存职位信息失败！");
            //将详细错误信息添加进msg
            msg.addData("fieldErrors", errorMap);
        }

        return msg;
    }

    /**
     * 该方法用于一次删除多个职位
     */
    @RequestMapping(value = "/positions", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteLotsPositions(@RequestParam List<Integer> idList) {
        Msg msg = null;

        int iResult = positionService.deleteLotsByPrimaryKey(idList);
        if (iResult > 0) {
            msg = Msg.succeed();
            msg.setMsg("删除成功，共删除" + iResult + "个职位");
        } else {
            msg = Msg.failed();
            msg.setMsg("删除失败!");
        }
        return msg;
    }


    /**
     * 该方法用更新于一个职位
     */
    @RequestMapping(value = "/positions", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updatePosition(@Valid @RequestBody Position position, BindingResult result) {
        Msg msg = null;

        //先检查职位信息有没有错误
        Map<String, Object> errorMap = positionService.checkPosition(position, result);
        System.out.println("传入的updatePosition为:" + position + "\nerrorMap:" + errorMap);
        //如果都无误，则尝试保存
        if (errorMap.size() < 1) {
            int iResult = positionService.updateByPrimaryKeySelective(position);
            if (iResult > 0) {
                msg = Msg.succeed();
                msg.addData("position_id", position.getId());
                msg.setMsg("更新成功，共更新" + iResult + "个职位");
            } else {
                msg = Msg.failed();
                msg.setMsg("更新职位信息失败!");
            }
        } else {
            msg = Msg.failed();
            msg.setMsg("更新职位信息失败！");
            //将详细错误信息添加进msg
            msg.addData("fieldErrors", errorMap);
        }

        return msg;
    }

}
